﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Commons;
using DAL;
using System.Web.Security;
using DAL.Enums;
using SimpComm;

namespace Bll
{
    public class UserInfo
    {
        private static UserInfo instance;

        private UserInfo() { }

        public static UserInfo Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new UserInfo();
                }
                return instance;
            }
        }
        #region 声明变量

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserID
        {
            get
            {
                if (ContextFactory.Current.Session["UserID"] != null)
                    return ConvertHelper1.ToInt32(ContextFactory.Current.Session["UserID"], 0);
                return 0;
            }
            set { ContextFactory.Current.Session.Add("UserID", value); }
        }

        /// <summary>
        /// 用户权限
        /// </summary>
        public int UserRole
        {
            get
            {
                if (ContextFactory.Current.Session["UserRole"] != null)
                    return ConvertHelper1.ToInt32(ContextFactory.Current.Session["UserRole"], 0);
                return 0;
            }
            set { ContextFactory.Current.Session.Add("UserRole", value); }
        }

        /// <summary>
        /// 管理员姓名
        /// </summary>
        public string UserName
        {
            get
            {
                if (ContextFactory.Current.Session["UserName"] != null)
                    return ContextFactory.Current.Session["UserName"].ToString().Trim();
                return null;
            }
            set { ContextFactory.Current.Session.Add("UserName", value); }
        }


        #endregion

        /// <summary>
        /// 是否登录有效
        /// </summary>
        /// <returns></returns>
        public bool IsAuthenticated
        {
            get
            {
                if (UserID > 0)
                    return true;
                return false;
            }
        }

        public int UserMsgCount { get; set; }

        #region 登录类
        /// <summary>
        /// 登录类
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="lx">保存时间，0：不保存</param>
        /// <returns></returns>
        public MsgBox Login(string Security_LoginID, string Security_LoginPwd, bool IsWriteLog = true)
        {
            Security_LoginPwd = MD5Util.GetMD5_32(Security_LoginPwd);
            if (string.IsNullOrWhiteSpace(Security_LoginID) || string.IsNullOrWhiteSpace(Security_LoginPwd))
                return new MsgBox(false, "账号密码不能为空或者非法值");
            User User = new UserService().Dal.GetEntity(p => p.Security_LoginID == Security_LoginID && p.Security_LoginPwd == Security_LoginPwd);
            if (User == null)
            {
                return new MsgBox(false, "密码或账号不正确.");
            }
            if (IsWriteLog)
            {
                var n = new Log_Login_User()
                            {
                                Action = emUserLogAction.LoginIn.Value,
                                UserID = User.ID,
                            };
                //TODO:耶耶哦哦
                //new User_Login_LogService().Insert(n);
            }
            UserID = User.ID;
            UserName = User.Name;
            UserRole = User.RRole;
            return new MsgBox(true, "登录成功");
        }
        #endregion
    }

}